import os


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))


SECRET_KEY = 'hl=@#u==0p^6w_byv%#+zr$!(2v$^07#&jfnuh(y)=*rck&ke+'
DEBUG = True
ALLOWED_HOSTS = ['*']


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'myaccount.apps.MyaccountConfig',
    'course',
    'blog.apps.BlogConfig',
    'article.apps.ArticleConfig',
    'pure_pagination',
    'ckeditor',
    'captcha',
    'django.contrib.sitemaps',
    'django.contrib.sites',
    'comments',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
    'widget_tweaks',
    'mdeditor',
    'rest_framework',
    'corsheaders',

]

AUTHENTICATION_BACKENDS = (

    # auth 身份验证 与 allauth 无关
    'django.contrib.auth.backends.ModelBackend',
    # allauth 身份验证
    'allauth.account.auth_backends.AuthenticationBackend',
)

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'myblogs.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')]
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'blog.views.global_variable',
                'course.views.CourseList',
            ],
        },
    },
]

WSGI_APPLICATION = 'myblogs.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Email setting
EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
EMAIL_HOST = 'smtp.qq.com'
EMAIL_HOST_USER = '1365682653@qq.com'
EMAIL_HOST_PASSWORD = 'znnvzivxugbhjceg'
EMAIL_PORT = 465

SERVER_EMAIL = '1365682653@qq.com'
DEFAULT_FROM_EMAIL = '1365682653@qq.com'
ADMINS = (('谭思', '1365682653@qq.com'),)

EMAIL_USE_TLS = True

# Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/

LANGUAGE_CODE = 'zh-hans'

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

USE_TZ = True




#--------------在模板中要读取到预先配置文件夹及Url-----------------------#

# 如果使用自定义用户model user，为了让 Django 能够识别自定义的用户模型，必须在此处添加app.自定义表名
AUTH_USER_MODEL='myaccount.User'

#利用前缀 STATIC_URL的具体内容,来映射STATIC_ROOT
STATIC_URL = '/static/'

#当执行python manage.py collectstatic时, 收集的静态文件放在该目录下.
#django会将STATICFILES_DIRS下的所有文件以及admin所需要用到的js,css,image文件全都放到STATIC_ROOT目录下.
STATIC_ROOT = os.path.join(BASE_DIR, 'collect_static')

STATICFILES_DIRS = [
    os.path.join(BASE_DIR,'static')
]

#存放上传的文件
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # ‘media’ 为你定义的文件名

#网站最基本的SEO设置TDK
# 网站描述
SITE_DESCRIPTION = "EOSONES的个人网站，记录生活的瞬间，分享学习的心得"
# 网站关键词
SITE_KEYWORDS = "EOSONES,网络,IT,技术,博客,Python"
# 网站页面
SITE_END_TITLE = "EOSONES个人博客"





LOGIN_URL="/myaccount/profile/"

SITE_ID = 1

# django-pure-pagination 分页设置
PAGINATION_SETTINGS = {
    'PAGE_RANGE_DISPLAYED': 4, # 分页条当前页前后应该显示的总页数（两边均匀分布，因此要设置为偶数），
    'MARGIN_PAGES_DISPLAYED': 1, # 分页条开头和结尾显示的页数
    'SHOW_FIRST_PAGE_WHEN_INVALID': True, # 当请求了不存在页，显示第一页
}

# 搜索设置
HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.elasticsearch2_backend.Elasticsearch2SearchEngine',
        'URL': '',
        'INDEX_NAME': 'helloworld',
    },
}
HAYSTACK_SEARCH_RESULTS_PER_PAGE = 10
HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'


HAYSTACK_CONNECTIONS['default']['URL'] = 'http://127.0.0.1:9200/'

HAYSTACK_CUSTOM_HIGHLIGHTER = 'article.utils.Highlighter'


# 要求用户注册时必须填写email
ACCOUNT_EMAIL_REQUIRED = True


# 指定要使用的登录方法(用户名、电子邮件地址或两者之一)
ACCOUNT_AUTHENTICATION_METHOD = "username_email"
# 登录尝试失败的次数
ACCOUNT_LOGIN_ATTEMPTS_LIMIT =5

# 更改为True，用户一旦确认他们的电子邮件地址，就会自动登录
ACCOUNT_LOGIN_ON_EMAIL_CONFIRMATION =False

# 用户注册时是否需要用户输入两遍密码
ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICE =True


# 加强电子邮件地址的唯一性
ACCOUNT_UNIQUE_EMAIL =True
# 用户名允许的最小长度的整数
ACCOUNT_USERNAME_MIN_LENGTH =1


# 设置退出登录后跳转链接
ACCOUNT_LOGOUT_REDIRECT_URL ="/"
# 用户登出是否需要确认确认(True表示直接退出，不用确认；False表示需要确认)
ACCOUNT_LOGOUT_ON_GET =True

LOGIN_REDIRECT_URL = '/accounts/profile/'


# 全局分页配置
REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS':  'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 10  # 每页数目
}

# 配置允许跨站访问本站的地址
CORS_ORIGIN_ALLOW_ALL = True
# CORS_ORIGIN_WHITELIST = (
#     'localhost:8000',  # 请求的域名(此处仅在CORS_ORIGIN_ALLOW_ALL = False时有效)
# )

# 定义允许的匹配路径正则表达式
CORS_URLS_REGEX = '^.*$'

# 设置允许访问的方法
CORS_ALLOW_METHODS = (
    'GET',
    'POST',
    'PUT',
    'PATCH',
    'DELETE',
    'OPTIONS',
)

# 跨域允许证书
CORS_ALLOW_CREDENTIALS = True

# 设置允许的header
CORS_ALLOW_HEADERS = (
    'Klicen-Agent',
    'Content-Type',
    'X-Requested-With',
    'xyt-agent',
)